package org.example.traing.util;

import io.jsonwebtoken.*;
import org.example.traing.entity.User;

import java.util.Date;
import java.util.UUID;

public class JWT {

    private static long time = 1000*60*60*24;
    private static String signature = "admin";
    private static JwtBuilder jwtBuilder = Jwts.builder();


    public static String jwt(String uid){


        String jwtToken = jwtBuilder
                //header
                .setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS256")
                //payload
                .claim("uid",uid)
                .setSubject("admin-test")
                .setExpiration(new Date(System.currentTimeMillis()+time))
                .setId(UUID.randomUUID().toString())
                //Signature
                .signWith(SignatureAlgorithm.HS256,signature)
                //拼接
                .compact();

       return jwtToken;
    }


    public static Claims parse(String token){



        JwtParser jwtParser = Jwts.parser();
        Jws<Claims> claimsJws=jwtParser.setSigningKey(signature).parseClaimsJws(token);
        Claims claims = claimsJws.getBody();
        return claims;
    }
}
